﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using POSSystem.DAL;
using System.Collections.Specialized;
using System.Data.SqlClient;
using System.Data;

namespace POSSystem.BLL
{
    public class Suppliers: _Suppliers
    {
        public virtual void UpdateSupllierAmt(int supid,
                decimal totalAmt,
                decimal paidAmt)
        {
            ListDictionary parameters = new ListDictionary();

            parameters.Add(new SqlParameter("@supid", SqlDbType.Int, 0), supid);
            parameters.Add(new SqlParameter("@totalAmt", SqlDbType.Decimal, 0), totalAmt);
            parameters.Add(new SqlParameter("@paidAmt", SqlDbType.Decimal, 0), paidAmt);

            LoadFromSqlNoExec("updateSupllierAmt", parameters);
        }

        public static Suppliers GetAllActiveSuppliers()
        {
            try
            {
                Suppliers suppliersTb = new Suppliers();
                suppliersTb.Where.Deleted.Value = false;
                suppliersTb.Where.BranchID.Value = Branches.CurrentBranchID;
                suppliersTb.Query.Load();

                return suppliersTb;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("Suppliers.cs", "GetAllActiveSuppliers", ex.StackTrace);
                throw new Exception("Suppliers.GetAllActiveSuppliers(): " + ex.Message);
            }
        }

        public void AddNewSupplier(string supplierName, string address, string phone, string fax,
            string email)
        {
            try
            {
                this.AddNew();
                //fields
                this.Deleted = false;
                this.SupplierName = supplierName;
                this.Address = address;
                this.Phone = phone;
                this.Fax = fax;
                this.Email = email;
                this.LastModifiedDate = DateTime.UtcNow.AddHours(SystemVariables.SysVars.TimeZone);
                this.BranchID = Branches.CurrentBranchID;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("Suppliers.cs", "AddNewSupplier", ex.StackTrace);
                throw new Exception("Suppliers.AddNewSupplier(): " + ex.Message);
            }
        }

        public void UpdateSupplierInfo(int supplierID, string supplierName, string address, string phone, string fax,
            string email)
        {
            try
            {
                //this.Where.ID.Value = customerID;
                this.Filter = Suppliers.ColumnNames.ID + " = " + supplierID.ToString();
                this.SupplierName = supplierName;
                this.Address = address;
                this.Phone = phone;
                this.Fax = fax;
                this.Email = email;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("Suppliers.cs", "UpdateSupplierInfo", ex.StackTrace);
                throw new Exception("Suppliers.UpdateSupplierInfo(): " + ex.Message);
            }
        }

        public void SetAllDeleted() //set filed Deleted of all rows = true
        {
            if (RowCount > 0)
            {
                Rewind(); //move to first record 
                do
                {
                    //do something with the current row 
                    this.Deleted = true;
                } while (this.MoveNext());
                this.Save();
            }
        }
    }
}
